package com.niiwoo.civet.trade.service.dubbo.creditor;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;
import com.niiwoo.civet.trade.dao.entity.InvestStdConfig;
import com.niiwoo.civet.trade.dto.common.SimpleNotifyDTO;
import com.niiwoo.civet.trade.dto.request.creditor.TradableInvestOrderFilterRequestDTO;
import com.niiwoo.civet.trade.dto.response.RelationInvestStdConfigDTO;
import com.niiwoo.civet.trade.dto.response.RelationInvestStdConfigItemDTO;
import com.niiwoo.civet.trade.dto.response.creditor.CreditorConfigDTO;
import com.niiwoo.civet.trade.dto.response.creditor.CreditorQueryConditionDTO;
import com.niiwoo.civet.trade.enums.InvestStdProductTypeEnum;
import com.niiwoo.civet.trade.enums.TradeConfigEnum;
import com.niiwoo.civet.trade.service.creditor.CreditorConfigDubboService;
import com.niiwoo.civet.trade.service.local.configure.InvestStdConfigLocalService;
import com.niiwoo.civet.trade.service.local.creditor.TradableInvestOrderService;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service(version = "1.0.0")
public class CreditorConfigDubboServiceImpl implements CreditorConfigDubboService {

	@Autowired
	private TradableInvestOrderService tradableInvestOrderService;
	
	@Autowired
	private InvestStdConfigLocalService investStdConfigLocalService;

	@Override
	public SimpleNotifyDTO getTradableInvestOrderConditionNotify() {
		Map<String, String> map = tradableInvestOrderService.getTradableInvestOrderLimitCondition();
		
		SimpleNotifyDTO result = new SimpleNotifyDTO();
		if (map != null) {
        	if (map.containsKey(TradeConfigEnum.CREDITOR_QUALIFICATION_PROMPT_TEXT.getConfigKey())) //可转让债权条件弹窗文案
        		result.setContent(map.get(TradeConfigEnum.CREDITOR_QUALIFICATION_PROMPT_TEXT.getConfigKey()));
        }
		
		return result;
	}
	
	@Override
	public SimpleNotifyDTO getTradableInvestOrderRateNotify() {
		Map<String, String> map = tradableInvestOrderService.getTradableInvestOrderLimitCondition();
		
		SimpleNotifyDTO result = new SimpleNotifyDTO();
		if (map != null) {
        	if (map.containsKey(TradeConfigEnum.CREDITOR_RATE_PROMPT_TEXT.getConfigKey())) //转让费率弹窗文案
        		result.setContent(map.get(TradeConfigEnum.CREDITOR_RATE_PROMPT_TEXT.getConfigKey()));
        }
		
		return result;
	}
	
	@Override
	public CreditorConfigDTO getCreditorConfigs() {
		Map<String, String> map = tradableInvestOrderService.getTradableInvestOrderLimitCondition();
		
		CreditorConfigDTO result = new CreditorConfigDTO();
		if (map != null) {
        	if (map.containsKey(TradeConfigEnum.DEBT_ON_OFF.getConfigKey()))
        		result.setCreditorSwitch(Integer.valueOf(map.get(TradeConfigEnum.DEBT_ON_OFF.getConfigKey())));
        	if (map.containsKey(TradeConfigEnum.DEBT_APPLY_TIME_BEGIN.getConfigKey()))
        		result.setApplyTimeBegin(map.get(TradeConfigEnum.DEBT_APPLY_TIME_BEGIN.getConfigKey()));
        	if (map.containsKey(TradeConfigEnum.DEBT_APPLY_TIME_END.getConfigKey()))
        		result.setApplyTimeEnd(map.get(TradeConfigEnum.DEBT_APPLY_TIME_END.getConfigKey()));
        	if (map.containsKey(TradeConfigEnum.DEBT_MIN_HAND_PERIOD.getConfigKey()))
        		result.setMinHandPeriod(Integer.valueOf(map.get(TradeConfigEnum.DEBT_MIN_HAND_PERIOD.getConfigKey())));
        	if (map.containsKey(TradeConfigEnum.DEBT_MIN_REMAIN_PERIOD.getConfigKey()))
        		result.setMinRemainPeriod(Integer.valueOf(map.get(TradeConfigEnum.DEBT_MIN_REMAIN_PERIOD.getConfigKey())));
        	if (map.containsKey(TradeConfigEnum.DEBT_LIMIT_TRANSFER_DAYS.getConfigKey()))
        		result.setLimitTransferDays(map.get(TradeConfigEnum.DEBT_LIMIT_TRANSFER_DAYS.getConfigKey()));
        	if (map.containsKey(TradeConfigEnum.DEBT_TRANSFER_TIME_LIMIT.getConfigKey()))
        		result.setTransferTimeLimit(map.get(TradeConfigEnum.DEBT_TRANSFER_TIME_LIMIT.getConfigKey()));
        	if (map.containsKey(TradeConfigEnum.DEBT_MIN_TRANSFER_AMOUNT.getConfigKey()))
        		result.setMinTransferAmount(new BigDecimal(map.get(TradeConfigEnum.DEBT_MIN_TRANSFER_AMOUNT.getConfigKey())));
        	if (map.containsKey(TradeConfigEnum.DEBT_MAX_TRANSFER_COUNT.getConfigKey()))
        		result.setMaxTransferCount(Integer.valueOf(map.get(TradeConfigEnum.DEBT_MAX_TRANSFER_COUNT.getConfigKey())));
        	if (map.containsKey(TradeConfigEnum.DEBT_LIMIT_TRANSFER_TIMES.getConfigKey()))
        		result.setLimitTransferTimes(Integer.valueOf(map.get(TradeConfigEnum.DEBT_LIMIT_TRANSFER_TIMES.getConfigKey())));
        	if (map.containsKey(TradeConfigEnum.DEBT_TRANSFER_TAG_PERIOD.getConfigKey()))
        		result.setTransferTagPeriod(map.get(TradeConfigEnum.DEBT_TRANSFER_TAG_PERIOD.getConfigKey()));
        	
        	if (map.containsKey(TradeConfigEnum.DEBT_LAUNCH_ON_OFF.getConfigKey()))
        		result.setCreditorSubmitSwitch(Integer.valueOf(map.get(TradeConfigEnum.DEBT_LAUNCH_ON_OFF.getConfigKey())));
        	if (map.containsKey(TradeConfigEnum.CREDITOR_RATE_PROMPT_TEXT.getConfigKey()))
        		result.setCreditorRatePromptText(map.get(TradeConfigEnum.CREDITOR_RATE_PROMPT_TEXT.getConfigKey()));
        	if (map.containsKey(TradeConfigEnum.CREDITOR_QUALIFICATION_PROMPT_TEXT.getConfigKey()))
        		result.setCreditorQualificationPromptText(map.get(TradeConfigEnum.CREDITOR_QUALIFICATION_PROMPT_TEXT.getConfigKey()));
        }
		
		return result;
	}
	
	@Override
	public CreditorQueryConditionDTO getCreditorQueryCondition(String userId) {
		CreditorQueryConditionDTO result = new CreditorQueryConditionDTO();
		
		Byte productType = InvestStdProductTypeEnum.INITIATE_TRANSFER.getValue(); // 4 - 可转让
		List<RelationInvestStdConfigDTO> list = investStdConfigLocalService.findRelationConfigByProductType(productType);
		
		if (list != null && !list.isEmpty()) {
			result.setRelations(list);
		}
		
		return result;
	}

	@Override
	public List<RelationInvestStdConfigItemDTO> getCreditorRelationQueryCondition(TradableInvestOrderFilterRequestDTO request) {
		List<InvestStdConfig> list = investStdConfigLocalService.selectByRelationAndConfigType(request);
		
		if (list == null || list.isEmpty()) return null;
		
		List<RelationInvestStdConfigItemDTO> result = new ArrayList<>();
		RelationInvestStdConfigItemDTO dto = null;
		for (InvestStdConfig item : list) {
			dto = new RelationInvestStdConfigItemDTO();
			
			dto.setId(item.getId());
			dto.setProductType(item.getProductType());
			dto.setConfigType(item.getConfigType());
			dto.setByteVal(item.getByteVal());
			dto.setMinval(item.getMinval());
			dto.setMinvalCalSymbol(item.getMinvalCalSymbol());
			dto.setMaxval(item.getMaxval());
			dto.setMaxvalCalSymbol(item.getMaxvalCalSymbol());
			dto.setTermUnit(item.getTermUnit());
			dto.setSeq(item.getSeq());
			dto.setTitle(item.getTitle());
			
			result.add(dto);
		}
		
		return result;
	}

}